package com.ktgj.customer.domain.Insurance.dftModel;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;

public class PaySign
{
    public static String EncoderByMd5(String str)
            throws EncException
    {
        MessageDigest md5;
        byte[] data;
        try
        {
             md5 = MessageDigest.getInstance("MD5");
        }
        catch (NoSuchAlgorithmException e)
        {
            return null;
        }
        try
        {
            data = str.getBytes("utf-8");
        }
        catch (UnsupportedEncodingException e)
        {
            throw new EncException(1, "PaySign", "EncoderByMd5", "EncoderByMd5加密异常", null);
        }
        BASE64Encoder base64en = new BASE64Encoder();

        String newstr = base64en.encode(md5.digest(data));
        return newstr;
    }

    public String sign(String dataString, String key)
            throws EncException
    {
        String src = EncoderByMd5(dataString);
        PayEncrypt pe = new PayEncrypt();
        return pe.encryptMode(key, src);
    }

    public boolean verify(String dataString, String signString, String key)
            throws EncException
    {
        dataString = dataString.replace(" ", "+");
        signString = signString.replace(" ", "+");
        String destsrc = sign(dataString, key);
        if (destsrc.equals(signString)) {
            return true;
        }
        throw new EncException(1, "PaySign", "verify", "verify error", null);
    }
}
